# Contributor: Francesco Camuffo <dev@fmac.xyz>
# Maintainer: Francesco Camuffo <dev@fmac.xyz>
pkgname=faust
pkgver=2.60.3
pkgrel=2
_llvmver=16
pkgdesc="Functional programming language for realtime audio signal processing"
url="https://faust.grame.fr"
arch="all"
license="GPL-2.0-or-later"
# all faust* tools are bash
depends="bash"
depends_dev="$pkgname-tools=$pkgver-r$pkgrel"
makedepends="
	cmake
	libmicrohttpd-dev
	libsndfile-dev
	llvm$_llvmver-dev
	samurai
	"
subpackages="
	$pkgname-tools
	$pkgname-static
	$pkgname-doc
	$pkgname-dev
	$pkgname-vim::noarch
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/grame-cncm/faust/releases/download/$pkgver/faust-$pkgver.tar.gz
	busybox.patch
	gcc13.patch
	no-execinfo.patch
	usage.patch
	"

prepare() {
	default_prepare

	# XXX: if the tests compile this faust segfaults
	rm -v examples/physicalModeling/fds/2dKirchhoffThinPlate.dsp
}

build() {
	cmake -B "$pkgname"-build -G Ninja \
		-S build \
		-C build/backends/all.cmake \
		-C build/targets/all.cmake \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DINCLUDE_DYNAMIC=ON \
		-DINCLUDE_STATIC=OFF \
		-DINCLUDE_ITP=ON \
		-DLLVM_CONFIG=/usr/lib/llvm$_llvmver/bin/llvm-config \
		-Wno-dev
	cmake --build "$pkgname"-build
	make VERBOSE=1 -C tools/sound2faust sound2faust sound2file
}

check() {
	make interp llvm -C tests/compile-tests
}

package() {
	DESTDIR="$pkgdir" cmake --install "$pkgname"-build
	make VERBOSE=1 PREFIX=/usr DESTDIR="$pkgdir" install -C tools/sound2faust

	# remove support for platforms not likely wanted
	rm -rvf "$pkgdir"/usr/bin/faust2au \
		"$pkgdir"/usr/lib/ios-libsndfile.a \
		"$pkgdir"/usr/share/faust/AU \
		"$pkgdir"/usr/share/faust/android \
		"$pkgdir"/usr/share/faust/iOS \
		"$pkgdir"/usr/share/faust/smartKeyboard \
		"$pkgdir"/usr/share/faust/max-msp/sndfile
	# rename generically named usage script
	mv "$pkgdir"/usr/bin/usage.sh "$pkgdir"/usr/bin/faust-usage.sh

	install -Dm644 syntax-highlighting/faust.vim \
		-t "$pkgdir"/usr/share/vim/vimfiles/syntax/
}

doc() {
	default_doc

	amove usr/share/faust/examples
}

dev() {
	pkgdesc="$pkgdesc (development files)"
	depends="$depends_dev"
	amove \
		usr/bin/faust-config \
		usr/include/* \
		usr/lib/*.so
}

tools() {
	pkgdesc="$pkgdesc (tools)"
	amove usr/bin/encoderunitypackage \
		usr/bin/faust2* \
		usr/bin/filename2ident \
		usr/bin/sound2*
}

vim() {
	pkgdesc="$pkgdesc (vim syntax)"
	install_if="vim $pkgname=$pkgver-r$pkgrel"

	amove usr/share/vim/vimfiles
}

sha512sums="
cdb31a519f5615ecdb36336d67de524b10acfc6e5449fea1a5a154a37d29f028c3db1ac0ba5f3bb9e0a39e00057397724dd5ad711fbab2ceacfb4cc1dee3e011  faust-2.60.3.tar.gz
2e2aae4b3fe760e41b3a9b1b82edc9cfac674944efb41c296e1c71f80763cbadc72544e0cbd7f5048095322779c3d432838ea66d32376a29b95bb08fe8c25e96  busybox.patch
edb822664f2277e58af82f6e339668c11a822880928646fddc21f4c245e81b70f9bc5e2892008dfe5c56a23b46aa50c40607f6ccca3a42029b7a21b0bf317302  gcc13.patch
fb1ef67a0c1ba56ba98e5c85935566c263a7b51a44c8ffd1789f7379b9bd14cbb2b373392e70c1c9974163559dbed910be3af64ccd3198633758998dee5c7bb2  no-execinfo.patch
ac45a55400970952daf7918f48b3d13efd06d5b3ffc1fa41e71d83eb3f53b31031fec353ab74e40c66667dbc3c6b32b2b38249a7e85c890221374cada38d4285  usage.patch
"
